package com.whnk.web.controller.pm;

import java.io.IOException;
import java.sql.Blob;
import java.util.List;

import com.whnk.pm.dto.FileInfoDTO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.whnk.common.annotation.Log;
import com.whnk.common.base.AjaxResult;
import com.whnk.common.enums.BusinessType;
import com.whnk.framework.web.base.BaseController;
import com.whnk.framework.web.page.TableDataInfo;
import com.whnk.pm.domain.BaseInfo;
import com.whnk.pm.domain.FileInfo;
import com.whnk.pm.service.IBaseInfoService;
import com.whnk.pm.service.IFileInfoService;

/**
 * 人员附件 信息操作处理
 * 
 * @author whwhnk
 * @date 2019-06-13
 */
@Controller
@RequestMapping("/pm/fileInfo")
public class FileInfoController extends BaseController
{
    private String prefix = "pm/fileInfo";
	
	@Autowired
	private IFileInfoService fileInfoService;
	
	@Autowired
	private IBaseInfoService baseInfoService;
	
	@RequiresPermissions("pm:fileInfo:view")
	@GetMapping()
	public String fileInfo()
	{
	    return prefix + "/fileInfo";
	}
	
	/**
	 * 查询人员附件列表
	 */
	@RequiresPermissions("pm:fileInfo:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(FileInfo fileInfo)
	{
		startPage();
        List<FileInfo> list = fileInfoService.selectFileInfoList(fileInfo);
		return getDataTable(list);
	}
	
	/**
	 * 新增人员附件
	 */
	@GetMapping("/add/{empId}")
	public String add(@PathVariable("empId") String empId, ModelMap mmap) {
		BaseInfo baseInfo = baseInfoService.selectBaseInfoById(empId);
		mmap.put("baseInfo", baseInfo);
		return prefix + "/add";
	}
	
	/**
	 * 新增保存人员附件
	 */
	@RequiresPermissions("pm:fileInfo:add")
	@Log(title = "人员附件", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(FileInfoDTO fileInfoDTO) throws IOException {
		FileInfo fileInfo = new FileInfo();
		fileInfo.setEmpId(fileInfoDTO.getEmpId());
		fileInfo.setEmpNm(fileInfoDTO.getEmpNm());
		fileInfo.setEmpAtt(fileInfoDTO.getEmpAtt().getBytes());
		fileInfo.setEmpAttType(fileInfoDTO.getEmpAttType());
		if (fileInfoDTO.getEmpAtt() != null) {
			fileInfo.setEmpFileNm(fileInfoDTO.getEmpAtt().getOriginalFilename());
		} else {
			fileInfo.setEmpFileNm(fileInfoDTO.getEmpFileNm());
		}
		return toAjax(fileInfoService.insertFileInfo(fileInfo));
	}

	/**
	 * 删除人员附件
	 */
	@RequiresPermissions("pm:fileInfo:remove")
	@Log(title = "人员附件", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(fileInfoService.deleteFileInfoByIds(ids));
	}
	
	/**
	 * TODO 下载单个附件
	 * 
	 * @param fileId
	 * @return
	 */
	@RequiresPermissions("pm:fileInfo:dlFile")
    @PostMapping("/dlFile")
    @ResponseBody
    public AjaxResult dlFile(Integer fileId)
    {
		FileInfo fileInfo = fileInfoService.selectFileInfoById(fileId);
        return AjaxResult.success(fileInfo.getEmpFileNm());
    }
	
	/**
	 * TODO 下载证件号码为empId的人员的所有附件
	 * 
	 * @param empId
	 * @return
	 */
	@RequiresPermissions("pm:fileInfo:dlFilesForEmp")
    @PostMapping("/dlFilesForEmp")
    @ResponseBody
    public AjaxResult dlFilesForEmp(String empId)
    {
		String fileName = fileInfoService.selectFilesByEmpId(empId);
        return AjaxResult.success(fileName);
    }
	
}
